package com.heima.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.admin.mapper.AdChannelMapper;
import com.heima.admin.service.AdChannelService;
import com.heima.model.admin.dtos.ChannelDto;
import com.heima.model.admin.pojo.AdChannel;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

@Service
public class AdChannelServiceImpl extends ServiceImpl<AdChannelMapper, AdChannel> implements AdChannelService {

    @Override
    public ResponseResult findByNameAndPage(ChannelDto dto) {
        //1.检查参数
        if (dto == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        dto.checkParam();
        //2.基于myBits-plus的分页查询
            //2.1 封装分页条件
        IPage page = new Page(dto.getPage(),dto.getSize());
            //2.2 封装查询条件
        LambdaQueryWrapper<AdChannel> queryWrapper = Wrappers.<AdChannel>lambdaQuery();
        //判断频道名称是否存在
        if (StringUtils.isNotBlank(dto.getName())) {
            queryWrapper.like(AdChannel::getName, dto.getName());
        }
        IPage<AdChannel> pageResult = this.page(page, queryWrapper);
        //3.得到查询结果，封装PageResponseResult返回
        PageResponseResult result = new PageResponseResult(dto.getPage(), dto.getSize(), pageResult.getTotal(), pageResult.getRecords());

        return result;
    }

    @Override
    public ResponseResult insert(AdChannel channel) {
        //1.先校验参数
        if (channel == null || StringUtils.isBlank(channel.getName())) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID, "频道名称不能为空");
        }
        //2.根据名称查询数量
        int count = this.count(Wrappers.<AdChannel>lambdaQuery().eq(AdChannel::getName, channel.getName()));
        if (count > 0) {
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_EXIST, "频道名称已存在");
        }
        //3.保存频道
        this.save(channel);

        //4.返回结果
        return ResponseResult.okResult();
    }

    @Override
    public ResponseResult update(AdChannel channel) {
        //1.检查参数
        if (channel == null || channel.getId() == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID, "id值不能为空");
        }
        AdChannel adchannel = this.getById(channel);
        if (adchannel == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST, "频道信息不存在");
        }
        //2.判断名称是否变更和新名称是否已存在
        if (StringUtils.isNotBlank(channel.getName()) && !channel.getName().equals(adchannel.getName())) {
            int count = this.count(Wrappers.<AdChannel>lambdaQuery().eq(AdChannel::getName, channel.getName()));
            if (count > 0) {
                return ResponseResult.errorResult(AppHttpCodeEnum.DATA_EXIST, "该频道名称已存在");
            }
        }
        //3.修改频道信息
        this.updateById(channel);

        return ResponseResult.okResult();
    }

    @Override
    public ResponseResult deleteById(Integer id) {

        //1.检查id
        if (id == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //2.根据id查询频道信息
        AdChannel channel = this.getById(id);
        if (channel == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST, "频道信息不存在");
        }
        //3.判断频道的启用状态
        if (channel.getStatus()) {
            return ResponseResult.errorResult(AppHttpCodeEnum.ADMING_CHANNEL_STATUS_TRUE, "该频道当前状态为有效,不能删除");
        }
        //4.删除频道
        this.removeById(id);
        return ResponseResult.okResult();
    }

}
